package com.ruoyi.web.controller.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BsJingqu;
import com.ruoyi.system.domain.BsMenpiaoCategory;
import com.ruoyi.system.domain.dto.MenPiaoDto;
import com.ruoyi.system.domain.vo.MenPiaoListVo;
import com.ruoyi.system.domain.vo.MenPiaoVo;
import com.ruoyi.system.service.BsJingquService;
import com.ruoyi.system.service.BsMenpiaoCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BsMenpiaoController
 * @Description 描述
 * @Author HanWL
 * @Date 2023/10/17 22:14
 * @Version 1.0
 **/
@RestController
@Api(tags = "门票管理接口")
@RequestMapping("/bs/menpiao")
public class BsMenpiaoController extends BaseController {

    @Resource
    private BsMenpiaoCategoryService bsMenpiaoCategoryService;
    @Resource
    private BsJingquService bsJingquService;

    @ApiOperation("新增门票")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody MenPiaoVo menPiaoVo){
        boolean result = bsMenpiaoCategoryService.dealAndSave(menPiaoVo);
        return toAjax(result);
    }

    @ApiOperation("更新门票信息")
    @PutMapping("update")
    public AjaxResult<Void> update(@RequestBody MenPiaoVo menPiaoVo){
        // 更新逻辑：无法根据ID进行更新，只能通过降数据删除重建的方式
        boolean result = bsMenpiaoCategoryService.deleteAndSave(menPiaoVo);
        return toAjax(result);
    }

    @ApiOperation("查询景区门票配置信息")
    @GetMapping("/getByJingquId/{jingquId}")
    public AjaxResult<MenPiaoDto> getByJingquId(@PathVariable Long jingquId){
        MenPiaoDto menPiaoDto = bsMenpiaoCategoryService.getInfoByJingquId(jingquId);
        return AjaxResult.success(menPiaoDto);
    }

    @ApiOperation("门票列表")
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jingquName", value = "景区名称"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    public TableDataInfo<?> page(String jingquName, String startTime, String endTime){
        LambdaQueryWrapper<BsJingqu> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(jingquName)){
            lqw.eq(BsJingqu::getJingquName, jingquName);
        }
        if (StringUtils.isNotEmpty(startTime)){
            lqw.gt(BsJingqu::getJingquOpenTime, startTime);
        }
        if (StringUtils.isNotNull(endTime)){
            lqw.lt(BsJingqu::getJingquOpenTime, endTime);
        }
        lqw.eq(BsJingqu::getDelFlag, Constants.LOGIC_NOT_DELETE);
        List<MenPiaoListVo> resultList = new ArrayList<>();
        startPage();
        List<BsJingqu> list = bsJingquService.list(lqw);
        TableDataInfo dataTable = getDataTable(list);
        list.forEach(x -> {
            MenPiaoListVo vo = new MenPiaoListVo();
            int count = bsMenpiaoCategoryService.count(new LambdaQueryWrapper<BsMenpiaoCategory>().eq(BsMenpiaoCategory::getJingquId, x.getJingquId()));
            vo.setCategoryNum(count);
            vo.setCreateTime(x.getCreateTime());
            vo.setJingquName(x.getJingquName());
            vo.setUpdateBy(x.getUpdateBy());
            vo.setUpdateTime(x.getUpdateTime());
            vo.setJingquId(x.getJingquId());
            resultList.add(vo);
        });
        dataTable.setRows(resultList);
        return dataTable;
    }





}
